<template>
  <div class="container">
    <el-card>
      <!-- 第一行 -->
      <el-row>
        <el-col :span="12"
          >学科名称
          <el-input
            v-model="query.subjectName"
            placeholder="请输入内容"
          ></el-input>

          <el-button class="btn-clear" @click="clearInputText">清除</el-button>
          <el-button type="primary" @click="searchSubject">搜索</el-button>
        </el-col>
        <el-col :span="12">
          <el-button
            class="btn-add"
            type="success"
            icon="el-icon-edit"
            @click="showAddDialog"
            >新增学科</el-button
          >
        </el-col>
      </el-row>
      <!-- 提示框 -->
      <el-alert
        :title="`数据一共${counts}条`"
        type="info"
        show-icon
        :closable="false"
      >
      </el-alert>
      <!-- 表格 -->
      <el-table :data="subjectsList" style="width: 100%">
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="subjectName" label="学科名称"> </el-table-column>
        <el-table-column prop="username" label="创建者"> </el-table-column>
        <el-table-column prop="addDate" label="创建日期" width="180">
          <template slot-scope="scope">
            {{ scope.row.addDate | parseTimeByString }}
          </template>
        </el-table-column>
        <el-table-column prop="isFrontDisplay" label="前台是否显示">
          <template slot-scope="scope">
            <span v-if="scope.row.isFrontDisplay == 1"> 是</span>
            <span v-else>否</span>
          </template>
        </el-table-column>

        <el-table-column prop="twoLevelDirectory" label="二级目录">
        </el-table-column>
        <el-table-column prop="tags" label="标签"> </el-table-column>
        <el-table-column prop="totals" label="题目数量"> </el-table-column>
        <el-table-column label="操作" width="250">
          <template slot-scope="scope">
            <el-button type="text" @click="toCates(scope.row)"
              >学科分类</el-button
            >
            <el-button type="text" @click="toTags(scope.row)"
              >学科标签</el-button
            >
            <el-button type="text" @click="showEditDialog(scope.row)"
              >修改</el-button
            >
            <el-button type="text" @click="deleteById(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.page"
        :page-sizes="[4, 6, 8, 10]"
        :page-size="query.pagesize"
        layout=" prev, pager, next, sizes,jumper"
        :total="counts"
      >
      </el-pagination>
    </el-card>
    <!-- 新增弹框 -->
    <el-dialog
      title="新增学科"
      :visible.sync="addDialogVisible"
      width="40%"
      @close="closeAddDialog"
    >
      <!-- 表单区域 -->
      <el-form
        :model="addForm"
        :rules="addFormrules"
        ref="addFormRef"
        label-width="100px"
      >
        <el-form-item label="学科名称" prop="subjectName">
          <el-input v-model="addForm.subjectName"></el-input>
        </el-form-item>
        <!-- switch开关 -->
        <!--因为提交表单的时候需要是数字 而开关的v-model属性值默认是true或者false 用【active-value="1" inactive-value="0"】，此时的【active-value】值类型为string，如果要求【active-value】值类型为number时，必须在其前面加上:才是数字型。active-value是打开时候的值，inactive-value是关闭时候的值 -->
        <el-form-item label="是否显示" prop="isFrontDisplay">
          <el-switch
            :active-value="1"
            :inactive-value="0"
            v-model="addForm.isFrontDisplay"
            active-color="#13ce66"
            inactive-color="#ff4949"
           
          >
          </el-switch>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveAddDialog">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改弹框 -->
    <el-dialog
      title="修改学科"
      :visible.sync="editDialogVisible"
      width="40%"
      @close="closeEditDialog"
    >
      <!-- 表单区域 -->
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="学科名称" prop="subjectName">
          <el-input v-model="editForm.subjectName"></el-input>
        </el-form-item>
        <el-form-item label="是否显示" prop="isFrontDisplay">
          <el-switch
            :active-value="1"
            :inactive-value="0"
            v-model="editForm.isFrontDisplay"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEditDialog">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { list, add, update, remove } from "@/api/hmmm/subjects";
export default {
  data() {
    return {
      query: {
        page: 1,
        pagesize: 10,

        subjectName: "",
      }, //请求获取初始的学科列表的请求参数
      subjectsList: [], //获取到的学科列表
      counts: 0, //获取到数据总的条数，分页的时候需要用到
      addDialogVisible: false,
      addForm: {
        subjectName: "",
        isFrontDisplay: 1, //初始值是1 表示开关是true,打开状态
      },
      addFormrules: {
        subjectName: [
          { required: true, message: "请输入学科名称", trigger: "blur" },
        ],
      },
      editDialogVisible: false,
      editForm: {
        subjectName: "",
        isFrontDisplay: true,
      },
      editFormRules: {
        subjectName: [
          { required: true, message: "请输入学科名称", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getSubjectsList();
  },
  methods: {
    // 获取学科列表数据
    async getSubjectsList() {
      const res = await list(this.query);
      // console.log(res);
      if (res.status !== 200)
        return this.$message.error("获取学科列表数据失败");
      this.subjectsList = res.data.items;
      this.counts = res.data.counts;
    },
    // 分页pagesize改变触发的函数
    handleSizeChange(val) {
      this.query.pagesize = val;
      this.getSubjectsList();
    },
    // 分页page改变触发的函数
    handleCurrentChange(val) {
      this.query.page = val;
      this.getSubjectsList();
    },
    // 清空搜索框内容，记得要刷新页面，重新调用列表数据
    clearInputText() {
      this.query.subjectName = "";
      this.getSubjectsList();
    },
    //点击搜索按钮发请求 调用函数
    searchSubject() {
      this.getSubjectsList();
    },
    // 展示添加学科对话框
    showAddDialog() {
      this.addDialogVisible = true;
    },
    // 添加学科对话框的表单重置事件
    closeAddDialog() {
      this.$refs.addFormRef.resetFields();
    },

    // 点击新增对话框的确定按钮发请求
    saveAddDialog() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        const res = await add(this.addForm);
        // console.log(res);
        if (res.status !== 200)
          return this.$message.error("添加学科列表数据失败");
        this.addDialogVisible = false;
        this.getSubjectsList();
        // console.log(this.addForm.isFrontDisplay);
      });
    },
    // 展示修改按钮的对话框 并且把数据回填到表单里
    showEditDialog(row) {
      // console.log(row);
      this.editDialogVisible = true;
      this.editForm = row;
    },
    // 关闭修改对话框的重置事件
    closeEditDialog() {
      this.$refs.editFormRef.resetFields();
    },
    // 点击修改对话框的确定 就发起请求
    saveEditDialog() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return;
        const res = await update(this.editForm);
        // console.log(res);
        if (res.status !== 200)
          return this.$message.error("修改学科列表数据失败");
        this.getSubjectsList();
        this.editDialogVisible = false;
      });
    },
    // 删除操作  要有弹框
    async deleteById(row) {
      const confirmResult = await this.$confirm(
        "此操作将永久删除该学科, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (confirmResult !== "confirm") return this.$message.info("已取消删除");
      const { data: res } = await remove(row);
      // console.log(res);
      this.$message.success("删除成功");
      this.getSubjectsList();
    },
    // 点击学科分类跳转页面
    toCates(row) {
      // console.log(row);
      this.$router.push({
        path: "/subjects/directorys",
        query: { id: row.id, name: row.subjectName },
      });
    },
    //点击学科标签跳转页面
    toTags(row) {
      this.$router.push({
        path: "/subjects/tags",
        query: { id: row.id, name: row.subjectName },
      });
    },
  },
};
</script>

<style scoped lang='less'>
.container {
  min-width: 1366px;
  .el-card {
    margin: 20px;
    padding-top: 20px;
    .el-input {
      width: 35%;
    }
    .btn-add {
      margin-left: 450px;
    }
    .btn-clear {
      margin-left: 10px;
    }
    .el-alert {
      margin-top: 15px;
    }
    .el-table {
      margin-top: 15px;
      .el-button {
        margin: 0 5px;
      }
    }
    .el-pagination {
      margin-top: 20px;
      text-align: right;
    }
  }
  /deep/ .el-dialog__wrapper {
    .el-dialog__footer {
      text-align: center !important;
    }
    .el-dialog {
     .el-input__inner {
       width: 50%;
     }
    }
  }
}
</style>
